package com.mr.web;

import com.mr.bo.UserInfo;
import com.mr.config.JwtConfig;
import com.mr.pojo.User;
import com.mr.service.UserService;
import com.mr.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description: TODO
 * @Author kun
 * @Date 2020/8/26
 * @Version V1.0
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtConfig jwtConfig;

    @GetMapping("check/{data}/{type}")
    public ResponseEntity<Boolean> check(@PathVariable("data") String data,
                                         @PathVariable("type") Integer type){
        //  参数不全，返回失败的请求
        if(data ==null || type ==null){
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        Boolean resu=  userService.validNameAndPhone(data,type);

        return ResponseEntity.ok(resu);
    }



    @PostMapping("register")
    public ResponseEntity<Void> register(User user){
        boolean resu=  userService.register(user);
        //注册失败，返回400请求

        if(!resu){
            return  new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(null);
    }
    @GetMapping("phone")
    public ResponseEntity<Void> Veriphone(User user, @CookieValue("B2C_TOKEN") String token){

        try{
            //获得登陆用户数据
            UserInfo userInfo= JwtUtils.getInfoFromToken(token,jwtConfig.getPublicKey());
            user.setUsername(userInfo.getUsername());
            user.setId(userInfo.getId());
            boolean resu=userService.Veriphone(user);
            if(!resu){
                return  new ResponseEntity(HttpStatus.BAD_REQUEST);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.ok(null);
    }

    @GetMapping("ph")
    public ResponseEntity<Void> phon(@PathVariable("phone") String phone,
                                      @PathVariable("code") String code){

       Boolean resu=userService.Vercode(phone,code);

       if(!resu){
           return  new ResponseEntity(HttpStatus.BAD_REQUEST);
       }

        return ResponseEntity.ok(null);
    }


    @GetMapping("query")
    public ResponseEntity<User> query(
            @RequestParam("username") String username,
            @RequestParam("password") String password){
        User user=userService.query(username,password);
        if(user==null){
//           "查询结果为null" 返回400请求
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        return  ResponseEntity.ok(user);
    }


    /**
     * 发送手机验证码
     */
    @PostMapping("code")
    public ResponseEntity<Void> sendVerifyCode(String phone){

        Boolean boo = this.userService.sendVerifyCode(phone);

        if (boo == null || !boo){
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("put")
    public ResponseEntity<Void> put( User user, @CookieValue("B2C_TOKEN") String token){
        try{
            //获得登陆用户数据
            UserInfo userInfo= JwtUtils.getInfoFromToken(token,jwtConfig.getPublicKey());
            if(userInfo.getUsername().equals(user.getUsername())){
                user.setId(userInfo.getId());
                userService.put(user);
            }else {
                //用户名不对返回400
                return   new ResponseEntity(HttpStatus.BAD_REQUEST);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
    }

    @PutMapping("shou")
    public ResponseEntity<Void> shou( User user, @CookieValue("B2C_TOKEN") String token){
        try{
            //获得登陆用户数据
            UserInfo userInfo= JwtUtils.getInfoFromToken(token,jwtConfig.getPublicKey());
                user.setId(userInfo.getId());
                user.setUsername(userInfo.getUsername());
                userService.shou(user);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
    }

}
